<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $id
 * @property string $name
 * @property string $login
 * @property string $email
 * @property string $password
 * @property string $avatar
 * @property string $create_time
 * @property string $role
 * @property integer $active
 * @property string $login_time
 * @property string $last_login_time
 *
 * The followings are the available model relations:
 * @property AuthItem[] $tblAuthItems
 */
class ManageUsers extends BackendActiveRecord
{
    public $image;
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{users}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //array('name, login, email, password, create_time, role, login_time, last_login_time', 'required'),
            array('name, login, email, password, role', 'required','on'=>'create'),
            //array('avatar', 'file', 'types'=>'jpg, gif, png','on'=>'update'),

            array('active', 'numerical', 'integerOnly'=>true),
            array('name, login, email', 'length', 'max'=>50),
            array('password', 'length', 'max'=>32),
            array('role', 'length', 'max'=>20),
            array('email', 'email'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, login, email, password, avatar, create_time, role, active, login_time, last_login_time', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'tblAuthItems' => array(self::MANY_MANY, 'AuthItem', 'tbl_auth_assignment(userid, itemname)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'login' => 'Login',
            'email' => 'Email',
            'password' => 'Password',
            'avatar' => 'Avatar',
            'create_time' => 'Create Time',
            'role' => 'Role',
            'active' => 'Active',
            'login_time' => 'Login Time',
            'last_login_time' => 'Last Login Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('login',$this->login,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('avatar',$this->avatar,true);
        $criteria->compare('create_time',$this->create_time,true);
        $criteria->compare('role',$this->role,true);
        $criteria->compare('active',$this->active);
        $criteria->compare('login_time',$this->login_time,true);
        $criteria->compare('last_login_time',$this->last_login_time,true);

        return new CActiveDataProvider($this, array(
                                                   'criteria'=>$criteria,
                                              ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Users the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function getActiveOptions()
    {
        return array('0'=>'Не активен','1'=>'Активен');
    }
    public function getRoleOptions()
    {
        return CHtml::listData(Yii::app()->authManager->getRoles(), 'name','name');

    }


}
